#include "calculatorinterface.h"
#include "ui_calculatorinterface.h"
#include "calculatorprocessingmessage.h"
#include <QKeyEvent>
#include <QDebug>
#include <QRegExpValidator>

CalculatorInterface::CalculatorInterface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CalculatorInterface)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(395,320);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    setWindowTitle(tr("计算器"));
    m_calculator = new CalculatorProcessingMessage;

    QRegExp regExp2("[0-9()+(?(/) (\367)|)*%./-#]{1,}");
    QRegExpValidator *pRegExpValidator2 = new QRegExpValidator(regExp2,this);
    ui->lineEdit->setValidator(pRegExpValidator2);

    ui->lineEdit->setText(QString::number(0));

    connect(m_calculator, SIGNAL(display(QString)),
            this, SLOT(slotDisplay(QString)));
    connect(m_calculator, SIGNAL(DisplayResult(QString)),
            this, SLOT(slotDisplayResult(QString)));
    connect(m_calculator, SIGNAL(displayBackSpace(QString)),
            this, SLOT(slotDisplayBackSpace(QString)));
    connect(m_calculator, SIGNAL(DisplayError(QString)),
            this, SLOT(slotDisplayError(QString)));

    QWidget::installEventFilter(this);
    ui->lineEdit->installEventFilter(this);
    ui->labelError->installEventFilter(this);
}

CalculatorInterface::~CalculatorInterface()
{
    delete ui;
}

bool CalculatorInterface::eventFilter(QObject *target, QEvent *event)
{
    if (target == this || target == ui->lineEdit
     || target == ui->labelError)
    {
        //键盘事件
        if (event->type() == QEvent::KeyRelease)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            switch(keyEvent->KeyRelease)
            {
                case Qt::Key_Escape:
                    this->hide();
                    break;

                case Qt::Key_Equal:
                case Qt::Key_Return:
                case Qt::Key_Enter:
                    on_pushButton_Equal_clicked();
                    break;

                case Qt::Key_0:
                    on_pushButton_0_clicked();
                    break;

                case Qt::Key_1:
                    on_pushButton_1_clicked();
                    break;

                case Qt::Key_2:
                    on_pushButton_2_clicked();
                    break;

                case Qt::Key_3:
                    on_pushButton_3_clicked();
                    break;

                case Qt::Key_4:
                    on_pushButton_4_clicked();
                    break;

                case Qt::Key_5:
                    on_pushButton_5_clicked();
                    break;

                case Qt::Key_6:
                    on_pushButton_6_clicked();
                    break;

                case Qt::Key_7:
                    on_pushButton_7_clicked();
                    break;

                case Qt::Key_8:
                    on_pushButton_8_clicked();
                    break;

                case Qt::Key_9:
                    on_pushButton_9_clicked();
                    break;

                case Qt::Key_B:
                    on_pushButton_BackSpace_clicked();
                    break;

                case Qt::Key_C:
                    on_pushButton_Clear_clicked();
                    break;

                case Qt::Key_ParenLeft:
                    on_pushButton_LeftParentheses_clicked();
                    break;

                case Qt::Key_ParenRight:
                    on_pushButton_RightParentheses_clicked();
                    break;

                case Qt::Key_Asterisk:  //*
                    on_pushButton_Multiplication_clicked();
                    break;

                case Qt::Key_Plus:  //+
                    on_pushButton_Add_clicked();
                    break;

                case Qt::Key_Minus:  //-
                    on_pushButton_Subtract_clicked();
                    break;

                case Qt::Key_Period:  //.
                    on_pushButton_point_clicked();
                    break;

                case Qt::Key_Slash:  // /
                    on_pushButton_Division_clicked();
                    break;

                case Qt::Key_Backspace:
                    on_pushButton_BackSpace_clicked();
                    break;
            }
        }
    }
    return QWidget::eventFilter(target, event);
}

//void CalculatorInterface::keyPressEvent(QKeyEvent *ev)
//{
//    switch(ev->key())
//    {
//        case Qt::Key_Escape:
//            this->hide();
//        case Qt::Key_Equal:
//        case Qt::Key_Return:
//        case Qt::Key_Enter:
//            on_pushButton_Equal_clicked();
//            break;
//        case Qt::Key_0:
//            on_pushButton_0_clicked();
//            break;
//        case Qt::Key_1:
//            on_pushButton_1_clicked();
//            break;
//        case Qt::Key_2:
//            on_pushButton_2_clicked();
//            break;
//        case Qt::Key_3:
//            on_pushButton_3_clicked();
//            break;
//        case Qt::Key_4:
//            on_pushButton_4_clicked();
//            break;
//        case Qt::Key_5:
//            on_pushButton_5_clicked();
//            break;
//        case Qt::Key_6:
//            on_pushButton_6_clicked();
//            break;
//        case Qt::Key_7:
//            on_pushButton_7_clicked();
//            break;
//        case Qt::Key_8:
//            on_pushButton_8_clicked();
//            break;
//        case Qt::Key_9:
//            on_pushButton_9_clicked();
//            break;
//        case Qt::Key_B:
//            on_pushButton_BackSpace_clicked();
//            break;
//        case Qt::Key_C:
//            on_pushButton_Clear_clicked();
//            break;
//        case Qt::Key_ParenLeft:
//            on_pushButton_LeftParentheses_clicked();
//            break;
//        case Qt::Key_ParenRight:
//            on_pushButton_RightParentheses_clicked();
//            break;
//        case Qt::Key_Asterisk:  //*
//            on_pushButton_Multiplication_clicked();
//            break;
//        case Qt::Key_Plus:  //+
//            on_pushButton_Add_clicked();
//            break;
//        case Qt::Key_Minus:  //-
//            on_pushButton_Subtract_clicked();
//            break;
//        case Qt::Key_Period:  //.
//            on_pushButton_point_clicked();
//            break;
//        case Qt::Key_Slash:  // /
//            on_pushButton_Division_clicked();
//            break;
//        case Qt::Key_Backspace:
//            on_pushButton_BackSpace_clicked();
//            break;
//        default:
//            QWidget::keyPressEvent(ev);
//    }
//}

void CalculatorInterface::slotDisplay(QString str)
{
    ui->labelError->clear();
    if(ui->lineEdit->text() == "0")
    {
        QString text = str;
        if (text.right(1) != ".")
            text.replace(0, 1, "");
        ui->lineEdit->setText(text);
    }
    else
    {
        ui->lineEdit->setText(str);
    }
}

void CalculatorInterface::slotDisplayResult(QString str)
{
    ui->labelError->clear();
    ui->lineEdit->setText(str);
}

void CalculatorInterface::slotDisplayBackSpace(QString str)
{
    ui->labelError->clear();
    ui->lineEdit->setText(str);
}

void CalculatorInterface::slotDisplayError(QString str)
{
//    if (m_showLineEditError)
//    ui->labelError->show();
    ui->labelError->setText(str);
}

void CalculatorInterface::on_pushButton_0_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(0)));
}

void CalculatorInterface::on_pushButton_1_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(1)));
}

void CalculatorInterface::on_pushButton_2_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(2)));
}

void CalculatorInterface::on_pushButton_3_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(3)));
}

void CalculatorInterface::on_pushButton_4_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(4)));
}

void CalculatorInterface::on_pushButton_5_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(5)));
}

void CalculatorInterface::on_pushButton_6_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(6)));
}

void CalculatorInterface::on_pushButton_7_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(7)));
}

void CalculatorInterface::on_pushButton_8_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(8)));
}

void CalculatorInterface::on_pushButton_9_clicked()
{
    if (ClickEqual)
    {
        ui->lineEdit->clear();
        ClickEqual = false;
    }
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString::number(9)));
}

void CalculatorInterface::on_pushButton_point_clicked()
{
    ClickEqual = false;
    if ((ui->lineEdit->text().right(1) <= "9") && (ui->lineEdit->text().right(1) >= "0"))
        m_calculator->Acceptedclick(ui->lineEdit->text().append('.'));
    else
        m_calculator->Acceptedclick(ui->lineEdit->text().append("0."));
}

void CalculatorInterface::on_pushButton_Add_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('+'));
}

void CalculatorInterface::on_pushButton_Subtract_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('-'));
}

void CalculatorInterface::on_pushButton_Multiplication_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('\327'));
}

void CalculatorInterface::on_pushButton_Division_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('\367'));
}

//void CalculatorInterface::on_pushButton_Symbol_clicked()    //正负号
//{
//    ClickEqual = false;

//    m_calculator->Acceptedclick(ui->lineEdit->text().append('\261'));
//}

void CalculatorInterface::on_pushButton_LeftParentheses_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('\50'));
}

void CalculatorInterface::on_pushButton_RightParentheses_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('\51'));
}

void CalculatorInterface::on_pushButton_Percent_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('%'));
}

void CalculatorInterface::on_pushButton_Sqrt_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append(QString(0x221a)));
}

void CalculatorInterface::on_pushButton_Square_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('\262'));
}

void CalculatorInterface::on_pushButton_BackSpace_clicked()
{
    m_calculator->AcceptedBackSpace(ui->lineEdit->text());
}

void CalculatorInterface::on_pushButton_Clear_clicked()
{
    ui->lineEdit->clear();
    ui->lineEdit->setText("0");
}

void CalculatorInterface::on_pushButton_Equal_clicked()
{
    ClickEqual = true;
    m_calculator->AcceptedEqual(ui->lineEdit->text());

}

void CalculatorInterface::on_pushButton_Factorial_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('!'));
}

void CalculatorInterface::on_pushButton_nthPower_clicked()
{
    ClickEqual = false;
    m_calculator->Acceptedclick(ui->lineEdit->text().append('^'));
}
